<?php

class Core_Backend_Component_Design_View_Block_Create extends Core_Backend_Share_View_Form {

    protected function initialize() {
        $this->setTemplatePath('edit');
    }

    protected function buildToolbar($toolbar) {
        $group = $toolbar->createGroup('back');
        $group->addLink($this->getHistoryUrl('design/block/index'), $this->__('share.button.back'));
        $group = $toolbar->createGroup('save');
        $group->addButton($this->__('share.button.apply'))
                ->bindEvent('click', "Kdf.Ui.getForm('" . $this->getForm()->getId() . "').submit('" . $this->getUrl(NULL, 'apply') . "')");
        $group->addButton($this->__('share.button.save'))
                ->bindEvent('click', "Kdf.Ui.getForm('" . $this->getForm()->getId() . "').submit('" . $this->getUrl(NULL, 'save') . "')");
        parent::buildToolBar($toolbar);
    }

    protected function buildForm($form) {
        $this->getTemplate()->assign('positions', $this->getPositions());
        $model = $this->getModel(NULL);
        $form->setAction($this->getUrl())
                ->setId('module_form')
                ->addHiddenField('id', 'module[id]')
                ->addHiddenField('module_id', 'module[module_id]', $this->getModule()->getId())
                ->addHiddenField('area_id', 'module[area_id]', $this->getSession()->get('managed_area_id'));
        $form->addSelectField($this->__('share.label.enable'), 'enabled', 'module[enabled]', TRUE)
                ->bindData(Kd_Localize_Helper::getLanguageItems('status.boolean'))
                ->addRequiredRule();
        $form->addTextboxField($this->__('share.label.name'), 'name', 'module[name]', TRUE)->addRequiredRule();
        $form->addSelectField($this->__('design.label.page_template'), 'page_template_id', 'module[page_template_id]')
                ->setOption('--', '')
                ->bindData($this->getPageTemplates(), 'name', 'id')
                ->addRequiredRule();
        $form->addSelectField($this->__('design.label.page_position'), 'position', 'module[position]')
                ->setOption('--', '')
                ->addRequiredRule();
        $form->addSelectField($this->__('design.label.block_template'), 'block_template_id', 'module[block_template_id]', TRUE)
                ->setOption('--', '')
                ->bindData($this->getBlockTemplates(), 'name', 'id')
                ->addRequiredRule();
        $form->addSelectField($this->__('share.label.title_enable'), 'title_enabled', 'module[parameters][title_enabled]', TRUE)
                ->bindData(Kd_Localize_Helper::getLanguageItems('status.boolean'))
                ->addRequiredRule();
        $form->addTextboxField($this->__('share.label.title'), 'title', 'module[parameters][title]');
        $form->addTextboxField($this->__('design.label.id_prefix'), 'id_prefix', 'module[parameters][id_prefix]');
        $form->addTextboxField($this->__('design.label.css_prefix'), 'css_prefix', 'module[parameters][css_prefix]');
        $form->createTab('base_option', $this->__('design.label.base_option'),
                array('field_name', 'field_enabled', 'field_page_template_id', 'field_position', 'field_block_template_id', 'field_title_enabled', 'field_title', 'field_id_prefix', 'field_css_prefix'));
        $this->getModuleType()->setParameters(is_null($model) ? array() : $model->getParameters())
             ->buildConfig($form);
        if ($model) {
            $form->getInput('position')->bindData(
                $this->getPageTemplates()->getItemById($model->getPageTemplateId())
                        ->getPositions()->filterByEnabled(1)->load(),
                'name', 'code');
            $form->bindData($model->getAllData());
            $form->bindData($model->getParameters());
        }
        parent::buildForm($form);
    }

}
